<template>
  <div class="comments">
    <hmheader title="精彩评论">
      <span slot="left"
            class="iconfont iconjiantou2"
            @click="$router.back()"></span>
    </hmheader>
    <div class="lists">
      <!-- 一个item就是一个评论块 -->
      <div class="item"
           v-for='value in commentList'
           :key='value.id'>
        <div class="head">
          <img :src="value.user.head_img"
               alt />
          <div>
            <p>{{value.user.nickname}}</p>
            <span>{{value.create_date | offsetTimeFormat}}</span>
          </div>
          <!-- value就是当前回复所在的评论对象 -->
          <span @click='replayComment(value)'>回复</span>
        </div>

        <commentItem v-if='value.parent'
                     :parent='value.parent'
                     @send='replayComment'></commentItem>

        <div class="text">{{value.content}}</div>
      </div>
    </div>
    <commentFooter :article='post'
                   @refreshData='refreshData'
                   :comment='temp'
                   @reset='temp=null'></commentFooter>
  </div>
</template>

<script>
import hmheader from "@/components/hmheader.vue";
import { getPostCommentList, getPostById } from '@/apis/post.js'
import axios from '@/utils/myaxios.js'
import { offsetTimeFormat } from '@/utils/myfilters.js'
import commentItem from '@/components/commentItem.vue'
import commentFooter from '@/components/commentFooter.vue'
export default {
  components: {
    hmheader, commentItem, commentFooter
  },
  data () {
    return {
      commentList: [],
      post: {},
      temp: {} //  中间成员
    }
  },
  activated () {
    this.init()
  },
  filters: {
    offsetTimeFormat
  },
  methods: {
    // 回复评论
    replayComment (value) {
      this.temp = value
    },
    // 数据初始化：获取文章评论数据  获取文章数据
    async init () {
      let id = this.$route.params.id

      // 获取当前文章的评论数据
      let res = await getPostCommentList(id)
      console.log(res);
      this.commentList = res.data.data.map(v => {
        v.user.head_img = axios.defaults.baseURL + v.user.head_img
        return v
      })

      // 获取当前文章数据
      this.post = (await getPostById(id)).data.data
    },
    refreshData () {
      this.init()
      window.scrollTo(0, 0)
    }
  }
};
</script>

<style lang='less' scoped>
.comments {
  padding-bottom: 60px;
}
.lists {
  border-top: 5px solid #ddd;
  padding: 0 15px;
  .item {
    padding: 10px 0;
    border-bottom: 1px solid #ccc;
    .head {
      display: flex;
      justify-content: space-between;
      align-items: center;
      > img {
        width: 50/360 * 100vw;
        height: 50/360 * 100vw;
        display: block;
        border-radius: 50%;
      }
      > div {
        flex: 1;
        display: flex;
        flex-direction: column;
        margin-left: 10px;
        > span {
          font-size: 12px;
          color: #999;
          line-height: 25px;
        }
      }
      > span {
        color: #999;
        font-size: 13px;
      }
    }
    .text {
      font-size: 14px;
      color: #333;
      padding: 20px 0 10px 0;
    }
  }
}
</style>